<template>
  <div>
    <el-dialog
      title="入驻店铺到商城"
      :visible.sync="dialogVisible"
      width="600px"
      :close-on-click-modal="false"
      @close="close"
    >
      <el-form ref="form" v-loading="false" :model="form" :rules="rules" label-width="80px">
        <el-form-item
          label="店铺："
          prop="shopId"
          :rules="[
            { required: true, message: '请选择一家店铺入驻到商城'}
          ]"
        >
          <el-select v-model="form.shopId" filterable placeholder="请选择一家店铺（可通过名称搜索）">
            <template v-for="(item, index) in $store.state.user.manageShopListExcludeHeadquarters">
              <el-option
                v-if="!selectShopIds.includes(item.id)"
                :key="index"
                :label="item.name"
                :disabled="selectShopIds.includes(item.id)"
                :value="item.id"
              />
            </template>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round @click="hide">取 消</el-button>
        <el-button size="small" round type="primary" @click="submit">确定入驻</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addStore } from '@/views/wxaMallModule/api/shop'
export default {
  data() {
    return {
      // 校验规则
      rules: {},
      // 配置
      resolve: null,
      reject: null,
      // 状态
      loading: false,
      dialogVisible: null,
      // 数据
      selectShopIds: [],
      form: {
        shopId: null
      }
    }
  },
  methods: {
    /**
       * 弹框相关
       */
    open({ selectShopIds = [] }) {
      this.selectShopIds = selectShopIds
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
       * 数据组
       */

    /**
       * 按钮组
       */
    // 确 定
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          addStore(this.form)
            .then(res => {
              this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.resolve && this.resolve()
              this.hide()
            })
            .catch(err => {})
            .finally(() => {
              this.loading = false
            })
        }
      })
    }

    /**
       * 状态组
       */

    /**
       * 方法组
       */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select {
    width: 100%;
  }
</style>
